package modele;

import java.awt.Color;
import java.util.ArrayList;

import modele.WorldElem.AtmosphericState;
import modele.WorldElem.SecondaryState;
import modele.WorldElem.State;
import modele.worldAgent.Agent;

import controller.VisitorDraw;

public class Square implements Cloneable{

	private int posZ;
	private int posX;
	private int posY;
	private State state;
//	private State nextState;
	private AtmosphericState atmosphericState;
	
	
	public Square(int posZ, int posX, int posY, State state) {
		this.posZ = posZ;
		this.posX = posX;
		this.posY = posY;
		this.state = state;
//		this.nextState = (State) state.clone();
	}


	public void draw(){
		state.accept(new VisitorDraw());
	}
	
	
	public void setState(State s){
		state = s;
	}
	
	public State getState(){
		return state;
	}
	
	public int getStateValue(){
		return state.getState();
	}


//	public void setNextState(State nextState) {
//		this.nextState = nextState;
//	}
//
//
//	public State getNextState() {
//		return nextState;
//	}
//
//	public int getNextStateValue(){
//		return nextState.getState();
//	}
	
	
	

	public void setSecondaryState(SecondaryState ss){
		state.setSecondaryState(ss);
	}
	
	public SecondaryState getSecondaryState(){
		return state.getSecondaryState();
	}
	
	public int getSecondaryStateValue(){
		return state.getSecondaryStateValue();
	}
	
	
	
//	public void setNextSecondaryState(SecondaryState ss){
//		nextState.setSecondaryState(ss);
//	}
//	
//	public int getNextSecondaryStateValue(){
//		return nextState.getSecondaryStateValue();
//	}
//	
//	public SecondaryState getNextSecondaryState(){
//		return nextState.getSecondaryState();
//	}
	
	
	public AtmosphericState getAtmosphericState() {
		return atmosphericState;
	}


	public ArrayList<ArrayList<Square>> getSquareAround(World w, int radius ){
		ArrayList<ArrayList<Square>> result = new ArrayList<ArrayList<Square>>();
		Square[][] stateMap = w.getSquareMap();
		int begI = (posX-radius > 0 ? posX-radius : 0);
		int endI = (posX+radius < w.getWidth() ? posX+radius :  w.getWidth() - 1);
		int endJ = (posY+radius < w.getWidth() ?  posY+radius :  w.getWidth() - 1);
		int begJ = (posY-radius > 0 ? posY-radius : 0);
		for(int i = begI ; i <= endI ; i++ ){
			ArrayList<Square> tmp = new ArrayList<Square>();
			for (int j = begJ ; j <= endJ ; j++){
				tmp.add(stateMap[i][j]);
			}
			result.add(tmp);
		}
		return result;
	}

	
	public ArrayList<ArrayList<Square>> getSquareAround(Square[][] squareMap, int radius ){
		ArrayList<ArrayList<Square>> result = new ArrayList<ArrayList<Square>>();
		int width = squareMap.length;
		int begI = (posX-radius > 0 ? posX-radius : 0);
		int endI = (posX+radius <width ? posX+radius :  width - 1);
		int endJ = (posY+radius < width ?  posY+radius :  width - 1);
		int begJ = (posY-radius > 0 ? posY-radius : 0);
		for(int i = begI ; i <= endI ; i++ ){
			ArrayList<Square> tmp = new ArrayList<Square>();
			for (int j = begJ ; j <= endJ ; j++){
				tmp.add(squareMap[i][j]);
			}
			result.add(tmp);
		}
		return result;
	}
	public int getPosZ() {
		return posZ;
	}


	public int getPosX() {
		return posX;
	}


	public int getPosY() {
		return posY;
	}
	
	public int getAltitude(){
		return posZ + state.getAdditionalAltitude();
	}
	
//	public int getNextAltitude(){
//		return posZ + nextState.getAdditionalAltitude();
//	}
	public Color getColor(){
		return state.getColor(this);
	}
	
	
	public Object clone(){
	    Square clone = new Square(posZ, posX, posY, (State) state.clone() );
	    return clone;
	}
	
	
	public boolean hasTheSameView(Square s){
		return (s.getSecondaryStateValue() == getSecondaryStateValue()
				&& s.getStateValue() == getStateValue()
				&& s.getAltitude() == getAltitude());
	}
	
	
}
